#include "dominion_helpers.h"
#include "dominion.h"
#include "rngs.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

int checkUpdateCoins(int player, struct gameState *current, int bonus)
{
	int i, c=0, g=0, s=0;

	struct gameState test;
	memcpy (&test, current, sizeof(struct gameState));

	test.coins += bonus;	
	for (i = 0; i < test.handCount[player]; i++) {
		if (test.hand[player][i] == copper) {
			c++;
			test.coins += 1;
		} else if (test.hand[player][i] == silver) {
			s++;
			test.coins += 2;
		} else if (test.hand[player][i] == gold) {
			g++;
			test.coins += 3;
		} 
	}

	updateCoins(player, current, bonus);

	if (memcmp(&test, current, sizeof(struct gameState)) != 0)
	{
		printf("bonus: %d\n", bonus);
		printf("gold: %d silver: %d, copper: %d\n", g, s, c);
		printf("test.coins: %d, current.coins: %d\n\n", test.coins, current->coins);
	}
	return 0;
}

int test2()
{

	return 0;
}

int main()
{
	//Update coins accepts a player, gamestate and bonus (amt of coins)
	//should update the gamestate to reflect an increase in coins equal
	//to the bonus amt
	printf("Testing updateCoins:\n");

	struct gameState state;
	int p, bonus;

	SelectStream(2);
	PutSeed(3);
	int i, j, nHand;

	for (i = 0; i < 2000; i++) {
		state.coins = 0;
		p = floor(Random() * MAX_PLAYERS);
		nHand = floor(Random() * MAX_HAND);
		state.handCount[p] = nHand;
		for (j = 0; j < nHand; j++) {
			state.hand[p][j] = floor(Random() * 30);
		}
		bonus = floor(Random() * 20);

		checkUpdateCoins(p, &state, bonus);
	}

	int k[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall};
	
	struct gameState G;

	initializeGame(2, k, 1, &G);

	printf("coins: %d\n", G.coins);
	printf("handcount: %d\n", G.handCount[0]);

	G.hand[0][G.handCount[0]] = gold;
	G.handCount[0]++;
	updateCoins(0, &G, 0);


	printf("coins after gold: %d\n", G.coins);

	//Add cutpurse to hand and play card (+2 coins) check update after
	G.hand[0][G.handCount[0]] = cutpurse;
	G.handCount[0]++;

	playCard(G.handCount[0], 0, 0, 0, &G);
	updateCoins(0, &G, 0);

	printf("Tests Passed\n");
	return 0;
}
